package com.taptech.doufu.ui.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

import com.taptech.doufu.ui.activity.NovelSectionDetailsActivity;

/**
 * Created by longsiyang on 16/1/6.
 */
public class MyScrollView extends ScrollView {
    private NovelSectionDetailsActivity activity;

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private static final String TAG = MyScrollView.class.getSimpleName();

    /** @param l Current horizontal scroll origin.
      * @param t Current vertical scroll origin.
      * @param oldl Previous horizontal scroll origin.
      * @param oldt Previous vertical scroll origin.*/
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (onScrollChangeListener != null){
            onScrollChangeListener.onScrollChange(l,t,oldl , oldt);
        }
        if (((this.getScrollY()) + (this.getMeasuredHeight() *2)) >= this.getChildAt(0).getMeasuredHeight() + this.getMeasuredHeight() && onScrollToBottomListener != null){
            onScrollToBottomListener.onScrollToBottom();
        }
        if (onScrollStatusListener != null) {
            onScrollStatusListener.onScrolling();
            mHandler.removeCallbacksAndMessages(null);
            mHandler.sendEmptyMessageDelayed(0x01, 200);
        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                if(activity != null){
                    activity.isClickNOrPScroll = false;
                }
                getParent().requestDisallowInterceptTouchEvent(false);
                ((View) getParent()).onTouchEvent(ev);
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    public interface OnScrollChangeListener{
        void onScrollChange(int l, int t, int oldl, int oldt);
    }

    public interface OnScrollToBottomListener{
        void onScrollToBottom();
    }

    private OnScrollChangeListener onScrollChangeListener;
    private OnScrollToBottomListener onScrollToBottomListener;

    public void setOnScrollChangeListener(OnScrollChangeListener listener){
        this.onScrollChangeListener = listener;
    }

    public void setOnScrollToBottomListener(OnScrollToBottomListener onScrollToBottomListener) {
        this.onScrollToBottomListener = onScrollToBottomListener;
    }

    public void setDetailsActivity(NovelSectionDetailsActivity activity){
        this.activity = activity;
    }



    private OnScrollStatusListener onScrollStatusListener;

    public void setOnScrollStatusListener(OnScrollStatusListener onScrollStatusListener) {
        this.onScrollStatusListener = onScrollStatusListener;
    }

    private Handler mHandler = new Handler() {

        @Override public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    if (onScrollStatusListener != null) {
                        onScrollStatusListener.onScrollStop();
                    }
                    break;
            }
        }
    };

    @Override protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeCallbacksAndMessages(null);
    }

    public interface OnScrollStatusListener {
        void onScrollStop();

        void onScrolling();
    }

}
